<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Product;
use Illuminate\Http\Request;
use App\Models\Category;
class CategoryController extends Controller
{
    //返回所有启用的栏目
    function category(){
        $where = ['parent_id'=>0];
        $pCates = Category::with('childs')->select('id','name','cover')->where($where)->orderBy('sort','desc')->get()->toArray();
        $cate = [];
        foreach ($pCates as $key=>$v) {
            $child = Category::where("parent_id",$v['id'])->pluck('id')->toArray();
            $cate[$key] = ['cate_id' => $v['id'], 'name' => $v['name'],'cover'=>$v['cover']];
            $children = Product::whereIn('category_id',$child)->get();

            if($children == []) {
                $cate[$key]['ishaveChild'] = false;
            }else{
                $cate[$key]['ishaveChild'] = true;
                $cate[$key]['children'] = $children;
            }
        }
        $res['List'] = $cate;
        return $this->response(0,$res);
    }
}
